package com.nblog.interceptor.admin;

import com.jfinal.aop.Duang;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.nblog.auth.annotation.RequiresPermissions;
import com.nblog.auth.common.Logical;
import com.nblog.constant.Constant;
import com.nblog.model.SysUser;
import com.nblog.service.SysUserService;

public class LoginInterceptor implements Interceptor {
	SysUserService sysUserService=Duang.duang(SysUserService.class);
	@Override

	public void intercept(Invocation inv) {
		System.out.println("测试");
		RequiresPermissions requiresPermissions=inv.getMethod().getAnnotation(RequiresPermissions.class);
		boolean hasPermissions=true; 
		if(requiresPermissions==null){
			requiresPermissions=inv.getController().getClass().getAnnotation(RequiresPermissions.class);
		}
		if(requiresPermissions!=null){
			System.out.println("步骤1");
			try {
				SysUser sysUser=inv.getController().getSessionAttr(Constant.SYSUSER);
				if(sysUser==null){//没有登陆
						System.out.println("步骤2");
						hasPermissions=false;
						if(StrKit.notBlank(inv.getActionKey())&&!inv.getActionKey().equals("/")){
							inv.getController().redirect("/admin/login?url="+inv.getActionKey());
						}else{
							inv.getController().redirect("/admin/login");
						}
					}else{
						System.out.println("步骤3");
						inv.getController().setAttr(Constant.SYSUSER, sysUser);
						//判断是否有该资源的访问权限
						System.out.println(JsonKit.toJson(requiresPermissions.value()));
						System.out.println("当地actionkey:"+inv.getActionKey());
						System.out.println("当前用户拥有权限:"+JsonKit.toJson(sysUserService.getPermissionSets(sysUser.getId())));
						String[] values=requiresPermissions.value();
						if(values.length==1){
							System.out.println("步骤4");
							if(!StrKit.isBlank(values[0])){
								System.out.println("步骤5");
								values[0]=inv.getActionKey();
							}
							System.out.println("当前判断权限："+values[0]);
							if(!sysUserService.getPermissionSets(sysUser.getId()).contains(values[0])&&!sysUserService.isAdmin(sysUser.getId())){
								hasPermissions=false;
								System.out.println("步骤6");
								inv.getController().renderJson("没有权限");
								System.out.println("没有权限");
								if(inv.getActionKey().equals("/admin")||inv.getActionKey().equals("/admin/index")){
									hasPermissions=true;
								}else{
									inv.getController().render("/WEB-INF/views/common/auth.html");
								}
								
							}
						}else{
							if(requiresPermissions.logical().equals(Logical.AND)){
								for(String value : values){
										if(!sysUserService.getPermissionSets(sysUser.getId()).contains(value)){
											hasPermissions=false;
											break;
										}
								}
								if(!hasPermissions){
									if(sysUserService.isAdmin(sysUser.getId())){
										hasPermissions=true;
									}
								}
							}else{
								hasPermissions=false;
								for(String value : values){
									if(sysUserService.getPermissionSets(sysUser.getId()).contains(value)){
										hasPermissions=true;
										break;
									}
								}
							}
							if(!hasPermissions){
								if(sysUserService.isAdmin(sysUser.getId())){
									System.out.println("我是管理员");
									hasPermissions=true;
								}
							}
						}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if(hasPermissions){
			inv.invoke();
		}
		
	}

}
